﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Module.TCenter
{
    public partial class TrainInfo : System.Web.UI.Page
    {
        private int userId;
        private int cUserId;
        private int venuesId;
        protected string ModuleId;
        private Jy123.Model.TCenter_Main TCenter;
        private Jy123.Model.Module_Main module;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;
        private string[] strContent = { "我的中心", "中心升级", "训练设备", "组件排行", "训练信息", "组件维护", "商业活动" };
        private string[] strCss = { "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu3", "lmenu2", "lmenu2" };
        private string[] strHeaderCss = { "", "", "NavIcon", "" };
        private string[] strHref = { "", "", "", "", "", "", "" };

        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            if (!Page.IsPostBack)
            {
                cUserId = int.Parse(strUserIdFromCookie);
                LoadInitialValue();
            }
        }

        private void LoadInitialValue()
        {
            string key = "TCenterId";
            string venuesIdString = Request.QueryString[key];
            if (!string.IsNullOrEmpty(venuesIdString))
            {
                venuesId = int.Parse(venuesIdString);
                ModuleId = venuesIdString;
                Jy123.BLL.TCenter_Main BLLVenues = new Jy123.BLL.TCenter_Main();

                Jy123.BLL.Module_Main BLLModuleMain = new Jy123.BLL.Module_Main();
                Jy123.BLL.Club.CModule BLLCModule = new Jy123.BLL.Club.CModule();
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

                TCenter = BLLVenues.GetModel(venuesId);
                userId = TCenter.FUser.Value;
                user = BLLUser.GetModel(userId);
                level = BLLLevel.GetModel(TCenter.FLevel.Value);
                module = BLLModuleMain.GetModel(venuesId);
                BindHTML();
            }
            else
            {
                //找不到UserId，返回首页,目前暂时返回空值
            }
        }

        private void BindHTML()
        {
            string strHTML = Cache["traininfo"].ToString();
            Jy123.Web.AppCode.Module appModule = new Jy123.Web.AppCode.Module();
            string strHeader = Cache["header"].ToString();
            string headerString = appModule.BindHeader(strHeader, strHeaderCss);
            string strLeft = Cache["leftmenu"].ToString();
            string strLeftLi = Cache["leftmenuli"].ToString();
            string strTail = "?TCenterId=" + TCenter.ID.ToString();
            string[] strHref = { "MyTCenter.aspx" + strTail, "UpdateTCenter.aspx" + strTail, "TCenterAnnexe.aspx" + strTail, "TCenterRank.aspx" + strTail, "#", "TCenterRepair.aspx" + strTail, "TCenterHire.aspx" + strTail };
            string leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strHref, strCss);
            string strClub = Cache["contentclub"].ToString();
            string contentString = appModule.BindContentClub(strClub, module, TCenter, user, level);
            string strMain = Cache["mainmenu"].ToString();
            string strDiv = Cache["mainmenusubdiv"].ToString();
            string strLi = Cache["mainmenusubdivli"].ToString();
            string mainMenuString = appModule.BindMainMenu(strMain, strDiv, strLi, cUserId);

            strHTML = strHTML.Replace("$Header$", headerString);
            strHTML = strHTML.Replace("$MainMenu$", mainMenuString);
            if (cUserId == userId)
            {
                strHTML = strHTML.Replace("$LeftMenu$", leftMenuString);
            }
            else
            {
                strHTML = strHTML.Replace("$LeftMenu$", string.Empty);
            }
            strHTML = strHTML.Replace("$ContentClub$", contentString);
            strHTML = strHTML.Replace("$Footer$", Cache["footer"].ToString());
            strHTML = strHTML.Replace("$VenuesName$", TCenter.TName);
            strHTML = strHTML.Replace("$ModuleId$", TCenter.ID.ToString());
            strHTML = BindSupporterRecord(strHTML);
            string strObject = Cache["flashobject"].ToString();
            string strXML = string.Format("/Module/VenuesRace/XML/{0}/{1}.xml", TCenter.ID, Guid.NewGuid());
            strObject = strObject.Replace("$XML$", strXML);
            strObject = strObject.Replace("$Value$", "/Cache/Module/Flash/Court.swf");
            strHTML = strHTML.Replace("$Object$", strObject);
            string[] strRace = TCenter.TrainClub.Split(',');
            strHTML = strHTML.Replace("$RaceCount$", strRace.Length.ToString());
            Response.Write(strHTML);
        }

        private string BindSupporterRecord(string strHTML)
        {
            StringBuilder strScript = new StringBuilder();
            string strNone = "style='display:none;'";
            string strBlock = "style='display:block;'";
            Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
            Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            strScript.AppendFormat("<script type='text/javascript' language='javascript'>");

            string[] strClubId = TCenter.TrainClub.Split(',');
            if (strClubId.Length > 0)
            {
                int fCId = int.Parse(strClubId[0]);
                Model.Club_Main cClub = BLLClub.GetModel(fCId);
                if (cUserId == cClub.FUser.Value)
                {

                    strHTML = strHTML.Replace("$clubliststyle$", "display:block");
                }
                else
                {

                    strHTML = strHTML.Replace("$clubliststyle$", "display:none");
                }
            }
            for (int i = 0; i < 1; i++)
            {
                if (!string.IsNullOrEmpty(strClubId[i]))
                {
                    int cClubId = int.Parse(strClubId[i]);
                    Jy123.Model.Club_Main cClub = BLLClub.GetModel(cClubId);
                    Jy123.Model.User_Main cUser = BLLUser.GetModel(cClub.FUser.Value);
                    Jy123.Model.Public_Level cClubLevel = BLLLevel.GetModel(cClub.FLevel.Value);
                    string cClubInfo = string.Format("<div id='clubInfoDiv{0}' class='QD3_2GameSetL1'></div>", i.ToString());
                    string strCDiv = Cache["traininfobigdiv"].ToString();
                    strCDiv = strCDiv.Replace("$ClubInfo$", cClubInfo);
                    strCDiv = strCDiv.Replace("$RaceInfoDiv$", "raceInfoDiv" + i.ToString());
                    strCDiv = strCDiv.Replace("$UlId$", "propsInfoDiv" + i.ToString());

                    strHTML = strHTML.Replace("$Div0$", strCDiv);
                    string strCScript = "$(function(){fillhtml('clubInfoDiv" + i.ToString() + "','/Module/TCenter/TrainClubInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "&Mark=" + i.ToString() + "'+'&random='+ Math.random())});";
                    string strRScript = "$(function(){fillhtml('raceInfoDiv" + i.ToString() + "','/Module/TCenter/TrainInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "&Mark=" + i.ToString() + "'+'&random='+ Math.random())});"; ;
                    string strPScript = "$(function(){fillhtml('propsInfoDiv" + i.ToString() + "','/Module/TCenter/TrainProps.ashx?VenuesId=" + TCenter.ID.ToString() + "&CMark=" + i.ToString() + "'+'&random='+ Math.random())});";
                    strScript.Append(strCScript);
                    strScript.Append(strRScript);
                    strScript.Append(strPScript);

                    strHTML = strHTML.Replace("$Style$", strBlock);
                }
            }
            string strFScript = "$(function(){fillhtml('objectDiv','/Module/Venues/FillRaceFlash.ashx?VenuesId=" + TCenter.ID.ToString() + "'+'&random='+ Math.random())});";
            string strLiScript = "$(function(){fillhtml('raceUl','/Module/TCenter/TrainUserInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "'+'&random='+ Math.random())});";
            strScript.Append(strFScript);
            strScript.Append(strLiScript);
            strScript.AppendFormat("</script>");
            strHTML = strHTML.Replace("$Script$", strScript.ToString());
            strHTML = BindScript(strHTML);
            return strHTML;
        }

        private string BindScript(string strHTML)
        {
            StringBuilder strScript = new StringBuilder();
            string timeOut = "30000";
            strScript.Append("<script type='text/javascript' language='javascript'>");

            string strVarClubInfo = "var clubInfo0 = function(){fillhtml('clubInfoDiv0','/Module/TCenter/TrainClubInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "&Mark='+$('#cClubHidden').val()+'&random='+ Math.random());}; ";
            strScript.Append(strVarClubInfo);
            strScript.AppendFormat(" var c0 = setInterval(clubInfo0,{0}); c0;", timeOut);
            string strVarRaceInfo = "var raceInfo0 = function(){fillhtml('raceInfoDiv0','/Module/TCenter/TrainInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "&Mark='+$('#cClubHidden').val()+'&random='+ Math.random());}; ";
            strScript.Append(strVarRaceInfo);
            strScript.AppendFormat(" var r0 = setInterval(raceInfo0,{0}); r0;", timeOut);
            string strVarPropsInfo = "var propsInfo0 = function(){fillhtml('propsInfoDiv0','/Module/TCenter/TrainProps.ashx?VenuesId=" + TCenter.ID.ToString() + "&CMark='+$('#cClubHidden').val()+'&random='+ Math.random());}; ";
            strScript.Append(strVarPropsInfo);
            strScript.AppendFormat(" var p0 = setInterval(propsInfo0,{0}); p0;", timeOut);
            string strVarUserInfo = "var userInfo0 = function(){fillhtml('raceUl','/Module/TCenter/TrainUserInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "&CMark='+$('#cClubHidden').val()+'&random='+ Math.random());}; ";
            strScript.Append(strVarUserInfo);
            strScript.AppendFormat(" var u0 = setInterval(userInfo0,{0}); u0;", timeOut);
            strScript.AppendFormat("</script>");
            strHTML = strHTML.Replace("$InterValScript$", strScript.ToString());
            return strHTML;
        }
    }
}
